package edu.hrbeu.crc.common.helper;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import edu.hrbeu.crc.constant.StatusConstant;
import edu.hrbeu.crc.mapper.ShoppingItemMapper;
import edu.hrbeu.crc.mapper.ShoppingListMapper;
import edu.hrbeu.crc.pojo.entity.Category;
import edu.hrbeu.crc.pojo.entity.ShoppingItem;
import edu.hrbeu.crc.pojo.entity.ShoppingList;
import edu.hrbeu.crc.pojo.vo.ShoppingItemVO;
import edu.hrbeu.crc.service.CategoryService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ListItemHelper {

    @Resource
    private ShoppingListMapper shoppingListMapper;

    @Resource
    private ShoppingItemMapper shoppingItemMapper;

    @Resource
    private CategoryService categoryService;

    /**
     * 检查清单下所有商品是否购买，并更新清单状态
     * @param listId
     */
    public void checkAndUpdateListStatus(Integer listId) {
        // 查询未购买的商品数量
        Long count = shoppingItemMapper.selectCount(
                new QueryWrapper<ShoppingItem>()
                        .lambda()
                        .eq(ShoppingItem::getListId, listId)
                        .ne(ShoppingItem::getStatus, StatusConstant.ENABLE)
        );

        // 若count = 0则更新清单状态
        if (count == 0) {
            shoppingListMapper.update(null,
                    new UpdateWrapper<ShoppingList>()
                            .lambda()
                            .set(ShoppingList::getStatus, StatusConstant.ENABLE)
                            .eq(ShoppingList::getId, listId)
            );
        }
    }

    /**
     * 查询清单表中商品的信息（根据清单id查询）
     * @param listId
     * @return
     */
    public List<ShoppingItemVO> getByListId(Integer listId) {
        List<ShoppingItem> items = shoppingItemMapper.selectList(
                new QueryWrapper<ShoppingItem>()
                        .lambda()
                        .eq(ShoppingItem::getListId, listId)
                        .orderByDesc(ShoppingItem::getUpdateTime)
                        .orderByDesc(ShoppingItem::getId)
        );
        List<ShoppingItemVO> vos = BeanUtil.copyToList(items, ShoppingItemVO.class);
        // 查找分类名
        for (ShoppingItemVO vo : vos) {
            Category c = categoryService.getCategoryById(vo.getCategoryId());
            vo.setCategoryName(c.getName());
            vo.setCategoryLogo(c.getLogo());
        }

        return vos;
    }

    /**
     * 删除清单中的商品数据
     * @param listId
     */
    public void removeByListId(Integer listId) {
        shoppingItemMapper.delete(
                new QueryWrapper<ShoppingItem>()
                        .lambda()
                        .eq(ShoppingItem::getListId,listId)
        );
    }

    /**
     * 根据清单id更改商品状态
     * @param listId
     */
    public void updateItemStatus(Integer listId) {
        shoppingItemMapper.update(null,
                new UpdateWrapper<ShoppingItem>()
                        .lambda()
                        .set(ShoppingItem::getStatus, StatusConstant.ENABLE)
                        .eq(ShoppingItem::getListId, listId)
        );
    }
}
